#include <stdio.h>
#include <stdlib.h>
//
// Created by root on 24-7-20.
//
#include "./include/readfile.h"

long int get_file_len(FILE *fd) {
    fseek(fd, 0L,SEEK_END);
    long int size = ftell(fd);
    fseek(fd, -size,SEEK_END);
    return size;
}


struct fileinfo getfiledata_need_free(char *path, char *mode) {
    struct fileinfo file;

    char *filebuffer;
    FILE *fd = fopen(path, mode);

    long int size = get_file_len(fd);
    file.filelen = size;
    filebuffer = (char *) malloc(sizeof(char) * size);


    char c;
    unsigned long int offset = 0;
    if (fd != NULL) {
        //feof(file stream )文件指针到达文件末尾
        while (!feof(fd)) {
            //读文件
            c = fgetc(fd);
            filebuffer[offset] = c;
            offset++;
        }
    }
    // filebuffer[offset]=0;
    file.data_needfree = filebuffer;
    file.fd = fd;
    return file;
}


